package com.mystack;



/**
 * @BelongsProject: DataStructures
 * @BelongsPackage: com.mystack
 * @Author: 江岸
 * @CreateTime: 2020-12-18 22:52
 * @Description: 链表模拟栈
 */
public class ListStackDemo {
    public static void main(String[] args) {
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.push(new HeroNode(1,"A","aaa"));
        myLinkedList.push(new HeroNode(2,"B","bbb"));
        myLinkedList.push(new HeroNode(3,"C","ccc"));
        myLinkedList.push(new HeroNode(4,"D","ddd"));
//        myLinkedList.show();
//        System.out.println();
//        System.out.println(myLinkedList.pop());
//        System.out.println();
//        myLinkedList.show();
        while (true){
            try {
                System.out.println(myLinkedList.pop());
            }catch (Exception e){
                System.err.println(e.getMessage());
                break;
            }

        }
    }

}

//定义一个heroNode，每个HeroNode对象就是一个节点
class HeroNode {
    public int no;
    public String name;
    public String nickName;
    public HeroNode next;//指向下一个节点

    public HeroNode(int no, String name, String nickName) {
        this.no = no;
        this.name = name;
        this.nickName = nickName;
    }

    public HeroNode() {
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }
}

//定义一个单链表
class MyLinkedList {
    //初始化一个头节点，头节点不要动，不存放具体的数据
    private  HeroNode head = new  HeroNode(0, "", "");

    public  HeroNode getHead() {
        return this.head;
    }

    //普通增加，找到最后节点，改一下next的指向即可
    public void add( HeroNode heroNode) {
        //因为head不能动，因此需要一个辅助变量temp
         HeroNode temp = head;
        //遍历链表，找到最后
        while (true) {
            if (temp.next == null) {
                break;
            }
            //如果没有找到最后，则将temp后移
            temp = temp.next;
        }
        temp.next = heroNode;
    }

    //第二种添加方法，根据排名添加到指定位置(如果排名存在，则提示添加失败)
    public void addByOrder( HeroNode heroNode) {
         HeroNode temp = head;
        boolean flag = false;//表示添加的编号是否存在，默认为false
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.no > heroNode.no) {
                //找到地方，就在该temp后面插入
                break;
            } else if (temp.next.no == heroNode.no) {
                //编号已存在
                flag = true;
                break;
            }
            temp = temp.next;//后移即遍历
        }
        //判断flag
        if (flag) {
            System.out.printf("准备插入的英雄编号%d已经存在", heroNode.no);
        } else {
            //插入
            heroNode.next = temp.next;
            temp.next = heroNode;
        }


    }

    //修改
    public void update( HeroNode heroNode) {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
         HeroNode temp = head.next;
        boolean flag = false;
        while (true) {
            if (temp == null) {
                break;
            }
            if (temp.no == heroNode.no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            temp.name = heroNode.name;
            temp.nickName = heroNode.nickName;
        } else {
            System.out.println("没有这个编号的英雄");
        }
    }

    //删除
    public void delete(int no) {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
         HeroNode temp = head;
        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.no == no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
             HeroNode h;
            h = temp.next;
            temp.next = temp.next.next;
            h.next = null;
        } else {
            System.out.println("没有这个编号的英雄");
        }

    }

    //遍历
    public void show() {
         HeroNode temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
            System.out.println(temp);
        }
    }

    //模拟栈push
    public void push(HeroNode heroNode){
       heroNode.next = head.next;
       head.next = heroNode;
    }

    //模拟栈pop
    public HeroNode pop(){
        if (head.next ==null){
           throw new RuntimeException("栈空");
        }
        HeroNode temp = head.next;
        head.next = temp.next;
        temp.next = null;
        return  temp;
    }
}